package com.tancire.web.controller.app;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.tancire.common.annotation.Log;
import com.tancire.common.core.controller.BaseController;
import com.tancire.common.core.domain.AjaxResult;
import com.tancire.common.core.domain.entity.SysUser;
import com.tancire.common.core.redis.RedisCache;
import com.tancire.common.enums.BusinessType;
import com.tancire.common.utils.SecurityUtils;
import com.tancire.common.utils.TcrDateUtils;
import com.tancire.net.domain.NetUserActivity;
import com.tancire.net.service.INetUserActivityService;
import com.tancire.net.service.IPurseInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 会员活动量Controller
 *
 * @author chenchao
 * @date 2023-11-20
 */
@RestController
@RequestMapping("/net/app/activity")
public class NetUserActivityAppController extends BaseController {

    @Resource
    private INetUserActivityService netUserActivityService;

    @Resource
    private IPurseInfoService purseInfoService;

    @Resource
    private RedisCache redisCache;

    /**
     * 新增会员活动量
     */
    @PreAuthorize("@ss.hasPermi('net:user:union')")
    @Log(title = "观看视频广告", businessType = BusinessType.INSERT)
    @PostMapping("/view/video")
    public AjaxResult add(@RequestBody NetUserActivity netUserActivity) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 保存观看次数
        String dayStr = TcrDateUtils.getDayStr();
        NetUserActivity activityQuery = new NetUserActivity();
        activityQuery.setActDay(dayStr);
        activityQuery.setUserId(user.getUserId());
        List<NetUserActivity> netUserActivities = netUserActivityService.selectNetUserActivityList(activityQuery);
        if (ObjectUtil.isNotEmpty(netUserActivities)) {
            NetUserActivity activity = netUserActivities.get(0);
            Integer spAdv = activity.getSpAdv();
            if (null == spAdv) {
                spAdv = 0;
            }
            // 观看数量 增加1
            spAdv = spAdv + 1;
            activity.setSpAdv(spAdv);
            this.netUserActivityService.updateNetUserActivity(activity);
        } else {
            // 创建记录
            NetUserActivity activity = new NetUserActivity();
            activity.setUserId(user.getUserId());
            activity.setUserName(user.getNickName());
            activity.setSpAdv(1);
            activity.setShowFlag(0);
            activity.setActDay(dayStr);
            // 生成ID
            activity.setId(IdUtil.getSnowflakeNextId());
            this.netUserActivityService.insertNetUserActivity(activity);
        }
        // 更新积分
        // 获取配置积分增加参数
        String cacheObject = redisCache.getCacheObject("sys_config:sys.adv.viewVideo");
        BigDecimal addPoint = new BigDecimal(cacheObject);
        // 调用增加积分
        this.purseInfoService.addPoints(user.getUserId(), addPoint, "观看激励视频获取积分");
        return AjaxResult.success();
    }

    /**
     * 新增会员活动量
     */
    @PreAuthorize("@ss.hasPermi('net:user:union')")
    @Log(title = "每日签到", businessType = BusinessType.INSERT)
    @PostMapping("/punch/day")
    public AjaxResult punchDay(@RequestBody NetUserActivity netUserActivity) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 保存观看次数
        String dayStr = TcrDateUtils.getDayStr();
        NetUserActivity activityQuery = new NetUserActivity();
        activityQuery.setActDay(dayStr);
        activityQuery.setUserId(user.getUserId());
        List<NetUserActivity> netUserActivities = netUserActivityService.selectNetUserActivityList(activityQuery);
        if (ObjectUtil.isNotEmpty(netUserActivities)) {
            NetUserActivity activity = netUserActivities.get(0);
            activity.setPunchDay(1);
            this.netUserActivityService.updateNetUserActivity(activity);
        } else {
            // 创建记录
            NetUserActivity activity = new NetUserActivity();
            activity.setUserId(user.getUserId());
            activity.setUserName(user.getNickName());
            activity.setSpAdv(0);
            activity.setShowFlag(0);
            activity.setActDay(dayStr);
            activity.setPunchDay(1);
            // 生成ID
            activity.setId(IdUtil.getSnowflakeNextId());
            this.netUserActivityService.insertNetUserActivity(activity);
        }
        // 更新积分
        // 获取配置积分增加参数
        String cacheObject = redisCache.getCacheObject("sys_config:sys.day.punch");
        BigDecimal addPoint = new BigDecimal(cacheObject);
        // 调用增加积分
        this.purseInfoService.addPoints(user.getUserId(), addPoint, "签到获取积分");
        return AjaxResult.success();
    }

    /**
     * 查询会员活动量列表
     */
    @PreAuthorize("@ss.hasPermi('net:user:union')")
    @GetMapping("/today")
    public AjaxResult today() {
        // 初始化默认信息
        NetUserActivity activity = new NetUserActivity();
        String dayStr = TcrDateUtils.getDayStr();
        activity.setActDay(dayStr);
        activity.setSpAdv(0);
        activity.setPunchDay(0);
        // 查询记录
        NetUserActivity query = new NetUserActivity();
        // 会员ID
        Long userId = SecurityUtils.getLoginUser().getUserId();
        query.setUserId(userId);
        // 今天
        query.setActDay(dayStr);
        List<NetUserActivity> list = netUserActivityService.selectNetUserActivityList(query);
        if (ObjectUtil.isNotEmpty(list)) {
            activity = list.get(0);
        }
        return AjaxResult.success("查询成功", activity);
    }

}
